# -*- coding: utf-8 -*-
import time
from . import JiangHu


class Check(JiangHu.JiangHu):
    def __init__(self, thread_id, name, index, public_data,ignore_block):
        super().__init__(thread_id, name, index, public_data,ignore_block)
        self.log.info("xxxxx0")


    def first_init(self):
        self.hwnd = 0
        self.publicData["系统数据"]["巡检是否绑定"] = False
        self.publicData["大哥不在线次数"] = 0
        self.publicData["选人阶段卡住次数"] = 0
        self.publicData["跑马次数"] = 0
        self.publicData["进入房间等待次数"] = 0
        self.publicData["白屏次数"] = 0  # 暂时弃用

        self.publicData["boss头像出现次数"] = 0

        self.data["重新连接次数"] = 0
        self.data["离线次数"] = 0

    def run(self):
        self.log.info("巡检线程启动")
        self.first_init()

        while True:
            if self.flag == "WAIT" or self.flag == "PAUSE":
                self.log.info("check当前状态%s"%self.flag)
                time.sleep(1)
                continue
            try:
                res=self.Process()
                if res==1:
                    break
                time.sleep(3)
            except SystemExit:
                self.log.info("调用stop终止巡检运行")
                if self.flag == "EXIT":
                    break
            except BaseException:
                self.LogTraceback()
                break
        self.dm.UnBindWindow()

    def Process(self):

        self.log.info("开始巡检")
        self.log.debug("当前游戏阶段 " + self.publicData["系统"]["主要阶段"])
        check_start_time=time.time()
        self.dm.SetBlockTrue()
        time.sleep(0.01)# v0.1+ 取消
        # 归为鼠标坐标
        mx, my, res = self.dm.GetCursorPos()
        check_end_time = time.time()
        self.log.info("巡检-6.5 耗时 %s s" % (check_end_time - check_start_time))
        self.log.info("当前鼠标 %s %s" % (mx , my))
        if self.hwnd!=0:
            if mx < 0 or my < 0 or mx >= 1280 or my >= 720:
                mx = 1260
                my = 710
                self.dm.MoveTo(mx, my)
        check_end_time = time.time()
        self.log.info("巡检0 耗时 %s s" % (check_end_time - check_start_time))
        self.dm.MoveTo(mx, my)
        self.log.info("获取巡检前鼠标位置 %s,%s" % (mx, my))
        check_end_time = time.time()
        self.log.info("巡检1 耗时 %s s" % (check_end_time - check_start_time))
        if self.hwnd == 0:
            self.log.info("开始启动dota2")
            res = self.StartDota2()
            self.log.info("启动dota2结果 %s" % res)

        is_bind = self.dm.IsBind(self.hwnd)
        check_end_time = time.time()
        self.log.info("巡检2 耗时 %s s" % (check_end_time - check_start_time))
        if is_bind == 1:
            self.publicData["系统数据"]["巡检是否绑定"] = True
        else:
            # 已启动没绑定
            self.log.info("等待dota2加载")
            res = self.WaitDota2Load()
            if res == 0:
                self.log.info("dota2需要更新/无网络")
                self.modifyStage(main_stage="退出游戏")
                return
            self.log.info("巡检开始绑定dota2窗口")
            if mx < 0 or my < 0 or mx >= 1280 or my >= 720:
                mx = 1260
                my = 710
                self.dm.MoveTo(mx, my)
            res=self.BindDOTA2Game()
            if res==1:
                self.publicData["系统数据"]["巡检是否绑定"] = True
            else:
                self.log.info("绑定失败重启游戏")
                self.modifyStage(main_stage="退出游戏")
                self.publicData["是否需要重启"] = True
        check_end_time = time.time()
        self.log.info("巡检3 耗时 %s s" % (check_end_time - check_start_time))
        self.log_stage()
        self.LowProbabilityAnomalyStart(3)
        check_end_time = time.time()
        self.log.info("巡检4 耗时 %s s" % (check_end_time - check_start_time))

        is_get = self.dm.SearchPngClick(130, 130, 450, 180, "dota2\江湖大标题.bmp", 0.8, 1, 0, 0, 0, 0)
        if is_get == 1:
            self.log.info("检测到大标题重新进入房间")
            if self.publicData["基础配置"]["角色"] == "主c":
                self.modifyStage(main_stage="创建房间")
            else:
                self.modifyStage(main_stage="查找房间")
                self.retryInRoomList()

        check_end_time = time.time()
        self.log.info("巡检5 耗时 %s s" % (check_end_time - check_start_time))

        if self.publicData["系统"]["主要阶段"] in ["加载游戏"]:
            self.log.info("检查跑马自救失败")
            is_get = self.dm.SearchPngClick(580, 1, 700, 40, "存档\跑马自救失败.bmp|存档\跑马自救失败2.bmp", 0.8, 1, 0,0 ,  0, 0)
            if is_get == 1:
                self.log.info("跑马自救失败 dota2需要重启")
                self.modifyStage(main_stage="退出游戏")
                self.publicData["是否需要重启"] = True
                time.sleep(30)


        check_end_time = time.time()
        self.log.info("巡检6 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["查找房间","进入房间", "创建房间","发育","等待","存档等待","存档" ,"退出游戏"]:
            self.log.info("检查是否需要更新")
            is_get = self.dm.SearchPngClick(462, 267, 816, 452, "dota2\需更新.bmp|dota2\需更新2.bmp", 0.8, 1, 0, 1,
                                            128, 108)
            if is_get == 1:
                self.log.info("dota2需要更新")
                self.modifyStage(main_stage="退出游戏")
                self.publicData["是否需要重启"] = True
                time.sleep(30)



        check_end_time = time.time()
        self.log.info("巡检7 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["进入房间", "查找房间"]:
            is_get = self.dm.SearchPngClick(565, 350, 710, 417, "dota2\房间不存在错误.bmp", 0.8, 1, 0, 1, 50, 25)
            if is_get == 1:
                self.log.info("房间不存在错误")
                self.dm.LeftClick(1094, 115, 1)
                self.modifyStage(main_stage="查找房间")
            is_get = self.dm.SearchPngClick(1078, 51, 1112, 69, "dota2\无网络.bmp", 0.8, 1, 0, 1, 0, 600)
            if is_get == 1:
                self.log.info("网络连接异常")
                self.publicData["是否需要重启"] = True
                time.sleep(300)

        check_end_time = time.time()
        self.log.info("巡检8 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["基础配置"]["角色"]!="主c" and self.publicData["系统"]["主要阶段"] == "进入房间":
            # is_get = self.dm.SearchPngClick(1170, 680, 1200, 700,"dota2\一个人.bmp", 0.8,1,0,0,1, 1)
            is_get = self.dm.SearchPngClick(1040, 670, 1260, 710, "dota2\房主开始比赛.bmp|dota2\房主开始比赛2.bmp",
                                            0.8, 1, 0, 1, 0, -20)
            if is_get == 1:
                self.modifyStage(main_stage="查找房间")

        check_end_time = time.time()
        self.log.info("巡检9 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] == "进入房间":
            self.publicData["进入房间等待次数"] += 1
            if self.publicData["进入房间等待次数"] > 200:
                self.publicData["进入房间等待次数"] = 0
                self.log.info("进入房间等待次数太多 重新查找房间")
                self.modifyStage("查找房间")
        else:
            self.publicData["进入房间等待次数"] = 0

        check_end_time = time.time()
        self.log.info("巡检10 耗时 %s s" % (check_end_time - check_start_time))
        self.SurpriseDropConfirm()
        check_end_time = time.time()
        self.log.info("巡检11 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] == "加载游戏":
            is_get = self.dm.SearchPngClick(1040, 670, 1260, 710, "dota2\房主开始比赛.bmp|dota2\房主开始比赛2.bmp",
                                            0.8, 1, 0, 1, 0, -20)
            if is_get == 1:
                if self.publicData["基础配置"]["角色"] == "主c":
                    self.modifyStage(main_stage="创建房间")
                else:
                    self.modifyStage(main_stage="查找房间")
                    self.log.info("未进入游戏且大哥跑路")
            is_get = self.dm.SearchPngClick(0, 150, 95, 182, "存档\超时返回.bmp", 0.8, 1, 0, 0, 0, 0)
            if is_get == 1:
                self.ExitGame()
                if self.publicData["基础配置"]["角色"] == "主c":
                    self.modifyStage(main_stage="创建房间")
                else:
                    self.modifyStage(main_stage="查找房间")

        check_end_time = time.time()
        self.log.info("巡检12 耗时 %s s" % (check_end_time - check_start_time))
        is_get = self.dm.SearchPngClick(590, 1,680 , 20, "dota2\离线模式.bmp", 0.8, 1, 0, 0, 0, 0)
        if is_get==1:
            self.log.info("网络离线")
            self.data["离线次数"] +=1
        else:
            self.data["离线次数"] = 0
        if self.data["离线次数"] >10:
            self.publicData["是否需要重启"]=True
            self.modifyStage(main_stage="退出游戏")
        self.OcrCarryDeadNum()
        check_end_time = time.time()
        self.log.info("巡检13 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["进入房间", "加载游戏", "关卡选择", "人物选择", "分解", "发育", "等待", "存档", "等待黄陵","存档黄陵", "存档等待"]:
            is_getx = self.dm.SearchPngClick(450, 250, 900, 500,
                                            "dota2\已断开服务器连接.bmp|dota2\已断开服务器连接1.bmp|dota2\已断开服务器连接2.bmp|dota2\已断开服务器连接3.bmp|dota2\已断开服务器连接4.bmp|dota2\已断开服务器连接5.bmp", 0.9, 1, 0, 1,
                                            120, 90)
            if is_getx == 1:
                time.sleep(1)
                self.log.info("掉线 开始重连")
                self.publicData["是否掉线"] = True
            is_get = self.dm.SearchPngClick(1075, 660, 1200, 695, "dota2\重新连接.bmp|dota2\重新连接2.bmp", 0.9, 1,
                                            0, 2, 10, 10)
            self.log.info("重连")
            if is_get == 1:
                self.log.info("掉线 开始重连")
                self.publicData["是否掉线"] = True
                self.data["重新连接次数"] += 1
                self.log.info("重新连接次数 %s" % self.data["重新连接次数"])
                if self.data["重新连接次数"] > self.publicData["系统"]["重新连接最大次数"]:
                    self.modifyStage(main_stage="退出游戏")
            elif is_getx==1 and self.publicData["系统"]["主要阶段"] in [ "分解", "发育", "等待", "存档", "等待黄陵","存档黄陵", "存档等待"]:
                # 断开后没有重连
                self.log.info("x重连2")
                self.publicData["是否掉线"] = True
                is_get = self.dm.SearchPngClick(1075, 660, 1200, 695, "dota2\重新连接.bmp|dota2\重新连接2.bmp", 0.9, 10,
                                                0.5, 2, 10, 10)
                if is_get == 1:
                    self.log.info("掉线 开始重连")
                    self.data["重新连接次数"] += 1
                    self.log.info("重新连接次数 %s" % self.data["重新连接次数"])
                    if self.data["重新连接次数"] > self.publicData["系统"]["重新连接最大次数"]:
                        self.modifyStage(main_stage="退出游戏")
                else:
                    self.log.info("x重连3")
                    self.modifyStage(main_stage="退出游戏")

        check_end_time = time.time()
        self.log.info("巡检14 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["发育", "等待"]:
            self.log.info("检查小游戏确认")
            if self.publicData["基础配置"]["角色"] == "主c":
                self.dm.SearchPngClick(550, 300, 700, 350, "存档\小游戏标题.bmp", 0.9, 1, 0, 1, 135, 100)
            else:
                self.dm.SearchPngClick(500, 400, 800, 450, "存档\小游戏确认.bmp", 0.9, 1, 0, 1, 10, 10)

        check_end_time = time.time()
        self.log.info("巡检15 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["发育", "等待", "存档", "等待黄陵","存档黄陵", "存档等待"] and self.publicData["基础配置"]["角色"] !="主c":
            for i in range(3):
                self.log.info("大哥不在线次数 %s " % self.publicData["大哥不在线次数"])
                ret = self.WaitePauseEnd()
                if ret == 2:
                    # 暂停后恢复 才设置为0
                    self.publicData["大哥不在线次数"] = 0
                    break

        check_end_time = time.time()
        self.log.info("巡检16 耗时 %s s" % (check_end_time - check_start_time))
        is_get = self.dm.SearchPngClick(580, 470, 630, 520, "存档\地图点赞.bmp|存档\地图点赞2.bmp", 0.9, 1,  0, 0, 0, 0)
        if is_get==1:
            self.log.info("游戏评价")
            self.dm.LeftClick(609,499,1)
        check_end_time = time.time()
        self.log.info("巡检17 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["发育", "等待"]:
            self.log.debug("检查守家-被攻击")
            is_defense=False
            defense_count=30
            if self.publicData["基础配置"]["角色"] == "主c" or self.publicData["混子守家"] == 1:
                x, y, res = self.dm.FindColor(201, 462, 208, 561, "e2a713-050505", 1.0, 7)
                if res == 1:
                    is_defense=True
                    defense_count=10
            if self.publicData["基础配置"]["角色"] == "主c" and self.publicData["系统"]["主要阶段"] in ["发育"] and self.publicData["系统"]["次要阶段"] <7:
                is_get = self.dm.SearchPngClick(360, 343, 866, 371, "主线\技能_效果.bmp", 0.9, 1, 0, 1, 10, 10)
            self.log.debug("检查守家-boss进攻")
            if self.publicData["基础配置"]["角色"] == "主c":
                is_get = self.dm.SearchPngClick(373, 65, 470, 155, "存档\进攻boss头像.bmp", 0.8, 1, 0, 0, 0, 0)
                if is_get == 1:
                    self.publicData["boss头像出现次数"]+=1
                    if self.publicData["boss头像出现次数"] > 6 :
                        is_defense=True
                else:
                    self.publicData["boss头像出现次数"] = 0
                is_get = self.dm.SearchPngClick(532, 42, 709, 76, "存档\最终boss挑战计时.bmp", 0.8, 1, 0, 0, 0, 0,
                                                delta_color="303030")
                if is_get == 1:
                    if self.publicData["开始打金轮时间"]<150:
                        self.publicData["开始打金轮时间"]=time.time()
                    self.log.debug("最终boss,开始守家")
                    self.modifyStage(main_stage="等待")
                    is_defense = True


            if is_defense:
                self.log.info("cost_time_debug defense")
                self.log.info(defense_count)
                pause_flag = True
                if self.get_main_stage() == "发育" and self.get_second_stage() == 17:
                    pause_flag = False
                self.log.info(pause_flag)
                self.log.info(self.publicData["obj"]["game"].flag)
                self.FastDefense(pause_flag=pause_flag)
                self.log.info(pause_flag)
                self.log.info(self.publicData["obj"]["game"].flag)
                i=0
                while i <= defense_count:
                    i=i+1
                    self.log.info(i)
                    self.Challenge_Archive()
                    if self.publicData["系统"]["主要阶段"] in ["存档"]:
                        break
                    time.sleep(1)
                    is_get=self.dm.SearchPngClick(550, 300, 700, 350, "存档\小游戏标题.bmp", 0.8, 1, 0, 1, 135, 100)
                    if is_get==1:
                        break
                    if i%10==0:
                        self.DotaAttack(67, 645, 640, 200)
                    elif i%10==9:
                        self.GoHome()
                    is_get = self.dm.SearchPngClick(373, 65, 470, 155, "存档\进攻boss头像.bmp", 0.8, 1, 0, 0, 0, 0)
                    is_get2 = self.dm.SearchPngClick(532, 42, 709, 76, "存档\最终boss挑战计时.bmp", 0.8, 1, 0, 0, 0, 0,
                                                    delta_color="303030")
                    if is_get != 1:
                        i=i+3
                        if is_get2!=1:
                            i=i+5
                self.log.info(pause_flag)
                if pause_flag:
                    self.log.info(self.publicData["obj"]["game"].flag)
                    self.publicData["是否打断游戏进程"] = True
                    self.goonGame()
                    self.log.info(self.publicData["obj"]["game"].flag)

        check_end_time = time.time()
        self.log.info("巡检18 耗时 %s s" % (check_end_time - check_start_time))

        if self.publicData["系统"]["主要阶段"] in ["分解", "发育", "等待"]:
            now= time.time()
            if self.publicData["开始打金轮时间"] > 150 and (now -self.publicData["开始打金轮时间"]) >350.0:
                self.log.info("打金轮超时")
                self.modifyStage(main_stage="存档等待")
                self.publicData["开始打金轮时间"]=0.0


        check_end_time = time.time()
        self.log.info("巡检19 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in [ "分解", "发育", "等待", "存档",  "存档等待"]:
            self.log.debug("开始检查防守失败")
            # 被神兵选择挡住
            is_get = self.dm.SearchPngClick(850, 520, 1080, 580, "存档\失败退出游戏覆盖.bmp", 0.8, 1, 0, 0, 0, 0)
            if is_get == 1:
                self.dm.LeftClick(583, 421, 2)
                time.sleep(1)
            is_get = self.dm.SearchPngClick(850, 520, 1080, 580,
                                            "存档\失败退出游戏.bmp|存档\失败退出游戏3.bmp", 0.8, 1, 0, 0,
                                            0, 0)
            if is_get == 1:
                self.log.info("防守失败")
                self.modifyStage(main_stage="存档等待")

        check_end_time = time.time()
        self.log.info("巡检20 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["存档",  "存档等待"]:
            self.log.debug("开始检查防时间结束")
            is_get = self.dm.SearchPngClick(500, 640, 900, 720, "存档\成功退出游戏.bmp", 0.8, 1, 0, 0, 0, 0)
            if is_get == 1:
                self.log.info("游戏结束")
                self.modifyStage(main_stage="存档等待")

        check_end_time = time.time()
        self.log.info("巡检21 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["发育", "等待"]:
            self.Challenge_Archive()

        check_end_time = time.time()
        self.log.info("巡检22 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["存档"]:
            self.dm.SearchPngClick(880, 525, 1075, 600, "存档\挑战存档.bmp|存档\挑战存档2.bmp", 0.8, 1, 0, 1, 10,  10)

        check_end_time = time.time()
        self.log.info("巡检23 耗时 %s s" % (check_end_time - check_start_time))
        # 不检查分解
        if self.publicData["系统"]["主要阶段"] in ["发育", "等待", "存档", "存档等待"] and self.publicData["基础配置"]["角色"] !="主c":
            chief_online = 0
            self.log.debug("检查大哥掉线")
            # 防止头像被折叠
            self.dm.SearchPngClick(0, 150, 30, 190, "存档\玩家信息展开.bmp", 0.8, 1, 0, 1, 10, 10)
            is_get = self.CheckChiefOnline()
            if is_get == 1:
                chief_online = 1
            else:
                if self.publicData["系统"]["主要阶段"] in ["发育", "等待"]:
                    is_get = self.dm.SearchPngClick(20, 50, 90, 70, "存档\活动.bmp|存档\进击的boss.bmp", 0.8, 1, 0,0, 0, 0)
                    if is_get:
                        chief_online = -1
                is_get3 = self.dm.SearchPngClick(0, 80, 20, 150, "存档\玩家信息.bmp", 0.8, 1, 0, 0, 0, 0)
                if is_get3 == 0:
                    chief_online = -1
            if chief_online == 1:
                self.publicData["大哥不在线次数"] = 0
            elif chief_online == 0:
                self.publicData["大哥不在线次数"] += 1
                self.log.debug("大哥不在线次数 %s" % self.publicData["大哥不在线次数"])
            if self.publicData["大哥不在线次数"] > 3:
                self.ClickSafe()
            if self.publicData["大哥不在线次数"] > 10:
                self.publicData["大哥不在线次数"] = 0
                if self.publicData["基础配置"]["角色"] != "主c":
                    self.modifyStage(main_stage="存档等待")

        check_end_time = time.time()
        self.log.info("巡检2x 耗时 %s s" % (check_end_time - check_start_time))
        if self.publicData["系统"]["主要阶段"] in ["退出游戏"]:
            # 停止游戏线程 todo
            self.modifyStage(second_stage="")
            self.ExitGame()
            # 需要优化
            self.dm.MoveTo(mx, my)
            self.dm.SetBlockFalse()
            check_end_time = time.time()
            self.log.info("结束巡检1 耗时 %s s" % (check_end_time - check_start_time))
            return 1
        self.dm.MoveTo(mx, my)
        self.dm.SetBlockFalse()
        check_end_time = time.time()
        self.log.info("结束巡检2 耗时 %s s" % (check_end_time-check_start_time))





